/**
 * 增加菜品
 */

//引入Q模块
var Q = require('q');
var fs=require('fs');

//find入口函数
var index = function(getData, postData, config, _redis, db) {
	var deferred = Q.defer();
	var foodTypeList = '';

	db.select('storeOrder').then(function(storeOrder){
		
	}, failCallbacks);
	//获得商户所有的信息
	db.select('foodTypes').then(function(foodTypes) {
		foodTypeList = foodTypes;
		if(postData.client == 'service') {
			//获取商户的菜单
			return db.select('foodMenu');
		}
		else {
			//获取商户的已上架的菜单
			return db.select('foodMenu', {
				foodStatus: 'pushed'
			});
		}		
	}, function() {
		//从数据库读取商户菜品类型失败
		deferred.reject('-0067');
	}).then(function(data){
		if(data && data.length == 0) {
			deferred.reject([]);
			return;
		}
		var foodMenuData = [];
		for(var foodMenuIndex = 0;foodMenuIndex < data.length;foodMenuIndex++) {
			foodMenuData.push({
				foodId: data[foodMenuIndex].foodId,
				foodType: data[foodMenuIndex].foodType,
				foodName: data[foodMenuIndex].foodName,
				foodPrice: parseInt(data[foodMenuIndex].foodPrice),
				foodPhoto: data[foodMenuIndex].foodPhoto,
				foodStatus:  data[foodMenuIndex].foodStatus,
				foodTimestamp: data[foodMenuIndex].foodTimestamp
			});
			if(foodMenuIndex == data.length - 1) {
				deferred.resolve({
					foodMenuData: foodMenuData,
					foodTypeList: foodTypeList
				});
			}
		}
	},function(){
		//获取菜单列表失败
		deferred.reject('-0063');
	});
	return deferred.promise;	
}

//暴露接口
module.exports = {
	index: index
};

